<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class ArticleExpertLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $articleExpertModel;

    public function _initialize()
    {
        $this->articleExpertModel = D('ArticleExpert', 'Model');
    }

    /**
     * 获取牛人日报列表
     *
     * @param unknown $params
     * @param unknown $page
     * @param unknown $pageSize
     */
    public function getExpertArticleList($params, $page, $pageSize)
    {
        $condition = [];

        if (isset($params['status'])) {
            $condition['a.status'] = $params['status'];
        }
        if (isset($params['is_top'])) {
            $condition['a.is_top'] = $params['is_top'];
        }
        if ($params['article_title']) {
            $condition['a.article_title'] = ['like', "%{$params['article_title']}%"];
        }
        if ($params['attribute']) {
            $condition['a.article_attribute'] = $params['attribute'];
        }
        if ($params['nick_name']) {
            $condition['u.nick_name'] = ['like', "{$params['nick_name']}%"];
        }

        $startTime = strtotime($params['start_time']);
        $endTime = '';
        if (!empty($params['end_time'])) {
            $endTime = strtotime($params['end_time'] . ' 23:59:59');
        }

        if (!empty($startTime) && !empty($endTime)) {
            $condition['a.publish_time'] = ['between', [$startTime, $endTime]];
        } elseif (!empty($startTime)) {
            $condition['a.publish_time'] = ['gt', $startTime];
        } else if (!empty($endTime)) {
            $condition['a.publish_time'] = ['lt', $endTime];
        }

        $orderby = 'article_id DESC';
        if ($params['sort']) {
            $arr = explode('-', $params['sort']);
            $orderby = "{$arr[0]} $arr[1], article_id DESC";
        }

        $list = M('expert_article a','clcw_', 'DB_CLCW')
            ->field('a.article_id, a.article_title, a.user_id, a.comment, a.is_top, a.create_time, a.publish_time, a.cron_time, a.status, a.article_attribute, u.nick_name, a.pv, a.uv')
            ->join('clcw_user u ON a.user_id = u.user_id')
            ->where($condition)
            ->order($orderby)
            ->page($page, $pageSize)
            ->select();

        $total = M('expert_article a','clcw_', 'DB_CLCW')
            ->join('clcw_user u ON a.user_id = u.user_id')
            ->where($condition)
            ->count();

        return ['total' => $total, 'list' => $list];
    }

    /**
     * 获取牛人日报
     *
     * @param unknown $id
     */
    public function getExpertArticleById($id)
    {
        return $this->articleExpertModel->where('article_id=' . $id)->find();
    }

    public function saveExpertArticle($data)
    {
        $id = $data['id'];
        unset($data['id']);

        if ($id) {
            $data['update_time'] = NOW_TIME;
            return $this->articleExpertModel->where(['article_id' => $id])->save($data);
        } else {
            if (empty($data['status'])) {
                $data['status'] = 2;
            }
            $data['create_time'] = NOW_TIME;
            return $this->articleExpertModel->add($data);
        }

    }

    /**
     * 获取牛人列表
     */
    public static function getExpertUserList()
    {
        return M('user', 'clcw_', 'DB_CLCW')->field('user_id,nick_name,mobile')->where(['is_expert' => 1])->select();
    }

    /**
     * 获取牛人日报作者
     */
    public function getExpertAuthorById($userId)
    {

        return M('user', 'clcw_', 'DB_CLCW')->where(['user_id' => $userId])->find();

    }
}